package org.iabako.server.serviceimpl.business;

import org.iabako.client.navigation.NavigationController;
import org.iabako.client.ui.layout.widget.dialogbox.IabakoDialogBox;
import org.iabako.server.dao.business.ClientDAO;
import org.iabako.server.tools.BundleMessages;
import org.iabako.server.tools.ServerTools;
import org.iabako.shared.entity.business.Installment;
import org.iabako.shared.entity.business.Payment;
import org.iabako.shared.entity.business.Sale;
import org.iabako.shared.entity.business.SaleProductService;
import org.iabako.shared.exception.IabakoActionForbiddenException;
import org.iabako.shared.exception.IabakoPackageForbiddenException;
import org.iabako.shared.exception.IabakoStockException;
import org.iabako.shared.exception.IabakoUniqueConstraintException;
import org.iabako.shared.iservice.business.SaleEnMasseService;
import org.iabako.shared.iservice.business.SaleService;
import org.iabako.shared.querydefinition.dto.QueryResultDTO;
import org.iabako.shared.tools.GenericTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by jose on 07/03/15.
 */

@Service("saleEnMasseService")
public class SaleEnMasseServiceImpl extends AbstractEnMasseService implements SaleEnMasseService {

    @Autowired
    SaleService saleService;
    @Autowired
    ClientDAO clientDAO;
    @Autowired
    BundleMessages messages;

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void save(Map<Long, QueryResultDTO> clientList, Sale sale, List<SaleProductService> productsServices, List<Payment> payments, List<Installment> installments) throws IabakoUniqueConstraintException, IabakoPackageForbiddenException, IabakoActionForbiddenException, IabakoStockException {

        sale.setProductsServices(productsServices);
        sale.setPayments(payments);
        sale.setInstallments(installments);

        //additional control to prevent useless loading of hibernate session
        for (SaleProductService saleProductService : productsServices) {
            if (saleProductService.getProduct() != null) {
                if (clientList.size() * saleProductService.getQuantity() > saleProductService.getProduct().getQuantity()) {
                    String unit = saleProductService.getProduct().getPriceUnit().getLabel();
                    unit = GenericTools.isEmpty(unit) ? " " + messages.getLabel("stock_unit") : unit;

                    String stringQuantity = saleProductService.getProduct().getQuantity() == 0d
                            || saleProductService.getProduct().getQuantity() / new Double(saleProductService.getProduct().getQuantity()).intValue() == 1 ?
                            new Double(saleProductService.getProduct().getQuantity()).intValue() + ""
                            : ServerTools.formatAmount(sale.getEnterprise().getLanguage(), saleProductService.getProduct().getQuantity());

                    String message = messages.getLabel("stock_sale_not_enough_text",
                            saleProductService.getProduct().getName(), stringQuantity + "", unit);

                    IabakoDialogBox iabakoDialogBox = new IabakoDialogBox("stock_sale_not_enough_title", message, true, "action_forbidden_by_package_ok");
                    iabakoDialogBox.setCloseOnlySimplePopup(true);
                    NavigationController.getInstance().show(iabakoDialogBox);
                    return;
                }
            }
        }

        for (Long idClient : clientList.keySet()) {
            Sale newSale = sale.customClone();
            newSale.setClient(clientDAO.findById(idClient));
            saleService.save(false, newSale, newSale.getProductsServices(), newSale.getPayments(), newSale.getInstallments());
        }
    }
}
